<?php

if (!defined('MAX_ABSTRACT_LENGTH')) {
    define('MAX_ABSTRACT_LENGTH', 300);
}

$templateData = array(
    'title'             => $this->title,
    'resourceUri'       => $this->resourceUri,
    'description'       => $this->description,
    'descriptionHelper' => $this->descriptionHelper,
    'options'           => $this->options
);

if (isset($this->description['http://purl.org/dc/terms/creator'][0]['value'])) {
    $creator = $this->description['http://purl.org/dc/terms/creator'][0]['value'];
    $creatorInstance = new OntoWiki_Resource($creator, OntoWiki::getInstance()->selectedModel);
    $creatorDescription = $creatorInstance->getDescription();
    $creatorDescription = $creatorDescription[$creator];
    $creatorHelper = $creatorInstance->getDescriptionHelper();
    
    $title = $creatorInstance->getTitle();
    $affixes = array();
    if (isset($creatorDescription['http://lod2.eu/schema/partner'][0]['value'])) {
        $partner = $creatorDescription['http://lod2.eu/schema/partner'][0]['value'];
        $affixes[] = $creatorHelper->getTitle($partner);
    }
    
    if (isset($creatorDescription['http://xmlns.com/foaf/0.1/title'][0]['value'])) {
        $title = sprintf(
            '%s %s', 
            $creatorDescription['http://xmlns.com/foaf/0.1/title'][0]['value'], 
            $title);
    }
    
    if (isset($creatorDescription['http://lod2.eu/schema/nameAffix'][0]['value'])) {
        $affixes[] = $creatorDescription['http://lod2.eu/schema/nameAffix'][0]['value'];;
    }
    
    if (count($affixes) > 0) {
        $title .= sprintf(' (%s)', implode(', ', $affixes));
    }
}

$abstractContent = array();
if (isset($this->description['http://lod2.eu/schema/abstract'][0]['value'])) {
    $abstractContent[] = $this->description['http://lod2.eu/schema/abstract'][0]['value'];
} else if (isset($this->description['http://lod2.eu/schema/content'][0]['value'])) {
    $content = $this->description['http://lod2.eu/schema/content'][0]['value'];
    if (strlen($content) > MAX_ABSTRACT_LENGTH) {
        $content = substr($content, 0, MAX_ABSTRACT_LENGTH - 10)
                 . '…';
    }
    $abstractContent[] = $content;
}

if (isset($this->options['show_link']) && $this->options['show_link']) {
    $abstractContent[] = '<a href="' . $this->url($this->resourceUri) . '">read</a>';
}

?>
<div class="result">
    <h4><a href="<?php echo $this->url($this->resourceUri) ?>"><?php echo $title ?></a></h4>
    <?php if (isset($creatorDescription['http://xmlns.com/foaf/0.1/depiction'][0]['value'])): ?>
        <img style="float:left;margin-right:5px;margin-top:5px" height="75" src="<?php echo $creatorDescription['http://xmlns.com/foaf/0.1/depiction'][0]['value'] ?>" title="depiction of <?php echo  $creatorInstance->getTitle(); ?>"/>
    <?php endif; ?>
    <p><?php echo implode(PHP_EOL, $abstractContent); ?></p>
</div>